package cn.com.nes.site.action.crm;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import cn.com.nes.common.baiduface.BaiduFace;
import cn.com.nes.common.baiduface.Faceprint;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.service.crm.BaiduFaceService;

@Controller("baiduface")
@RequestMapping("/baiduface")
public class BaiduFaceAction {

	private BaiduFace baiduFace;
	
	@Resource
	private BaiduFaceService service;
	
	public BaiduFaceAction() {
		this.baiduFace = new BaiduFace("UjrGT5EsxXbXounhFAsDsYLz", "5G1KzAQRNuEoxUnfBnu9S6gbTNv2A88C", 14159);
	}
	
	private String getPath(HttpServletRequest request) {
		String fullPath = request.getRequestURL().toString();
		return fullPath.substring(0, fullPath.lastIndexOf("/"));
	}
	
	@RequestMapping(value="/getQR", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
	@ResponseBody
	public byte[] genetateQR(HttpServletRequest request, 
			@RequestParam(defaultValue="150") Integer width, 
			@RequestParam(defaultValue="150") Integer height, 
			@RequestParam String businessId) throws WriterException, IOException {
		String path = getPath(request);
		
		String verifyToken = baiduFace.generateVerifyToken();
		
		PageData pageData = service.create(verifyToken, businessId);
		if(pageData.getRetCode() != 0) {
			throw new RuntimeException(pageData.getRetMessage());
		};
		String successUrl = path + "/successed?token=" + verifyToken;
		String failedUrl = path + "/failed?token=" + verifyToken;
		String url = baiduFace.generateUrl(verifyToken, successUrl, failedUrl);
		
		QRCodeWriter qrCodeWriter = new QRCodeWriter();
		BitMatrix bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, width, height);
		
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		MatrixToImageWriter.writeToStream(bitMatrix, "PNG", output);
		return output.toByteArray();
	}
	
	@RequestMapping("/successed")
	public Object successed(@RequestParam String token, Model model) {
		PageData pageData = service.check(token);
		if(pageData.getRetCode() != 0) {
			model.addAttribute("message", pageData.getRetMessage());
			return "baiduface/successed";
		}
		Faceprint faceprint = baiduFace.parser(token);
		pageData = service.update(faceprint);
		if(pageData.getRetCode() != 0) {
			model.addAttribute("message", pageData.getRetMessage());
			return "baiduface/successed";
		}
		model.addAttribute("message", "实名认证成功");
		return "baiduface/successed";
	}
	
	@RequestMapping("/failed")
	public Object failed(@RequestParam String token, Model model) {
		PageData pageData = service.check(token);
		if(pageData.getRetCode() != 0) {
			model.addAttribute("message", pageData.getRetMessage());
			return "baiduface/failed";
		}
		Faceprint faceprint = baiduFace.parser(token);
		pageData = service.update(faceprint);
		if(pageData.getRetCode() != 0) {
			model.addAttribute("message", pageData.getRetMessage());
			return "baiduface/failed";
		}
		model.addAttribute("message", faceprint.getMessage());
		return "baiduface/failed";
	}
	
}
